define([], function () {
	var app = angular.module("App.home");
	app.controller("projects.detail.mileageCtrl", ["$scope", "$timeout", "curPrj", "showMsg", function ($scope, $timeout, curPrj, showMsg) {
		$scope.hasUploaded = false;
		
		curPrj
			.getMileage()
			.then(function () {
				$scope.hasUploaded = true;
			});

		var initUploader = function () {
			var uploader = Qiniu.uploader({
				runtimes: 'html5,flash,html4', // 上传模式，依次退化
				browse_button: 'excelUpload', // 上传选择的点选按钮，**必需**
				uptoken_url: '/token',
				unique_names: true,
				filter: {
					mime_types: [{
						title: "Excel Files",
						extensions: "xls,xlsx"
					}]
				},
				domain: 'http://7i7il6.com1.z0.glb.clouddn.com/',  
				container: 'container', // 上传区域DOM ID，默认是browser_button的父元素
				max_file_size: '100mb', // 最大文件体积限制
				flash_swf_url: 'javascripts/bin/plupload/Moxie.swf', // 引入flash，相对路径
				max_retries: 3, // 上传失败最大重试次数
				dragdrop: false, // 开启可拖拽上传
				chunk_size: '4mb', // 分块上传时，每片的体积
				auto_start: true, // 选择文件后自动上传，若关闭需要自己绑定事件触发上传
				init: {
					'FilesAdded': function (up, files) {
						if (files.length > 1) {
							showMsg("只允许上传一个文件！", "alert");
							return;
						}
					},
					'BeforeUpload': function (up, file) {
						$scope.status = "uploading";
					},
					'FileUploaded': function (up, file, info) {
						var domain = up.getOption('domain');
						info = JSON.parse(info);
						var uploadItem = {
							prjId: curPrj.prjId,
							fileName: info.key,
							url: domain + info.key
						};
						curPrj
							.postMileage(uploadItem)
							.then(function () {
								$scope.hasUploaded = true;
								$scope.status = "succeeded";
							}, function () {
								$scope.status = "failed";
							});
					},
					'Error': function (up, err, errTip) {
						alert('上传出错，刷新页面再试！');
					},
				}
			});
			return uploader;
		};
		$timeout(initUploader, 400);
	}]);
});